import axios from 'axios'
import  type { AxiosResponse } from 'axios'

// 定义响应数据类型
interface AnimeInfo {
  atime: number;
  bg: string;
  desc: string;
  id: string;
  logo: string;
  month: number;
  title: string;
  year: number;
  bg_bak: string;
  logo_bak: string;
}

interface RecommendMusic {
  recommend: boolean;
  atime: number;
  title: string;
  anime_info: AnimeInfo;
  id: string;
  play_url: string;
  type: string;
  author: string;
  bangumi_id: number;
}

interface RecommendResponse {
  code: number;
  msg: string;
  res: RecommendMusic;
}

// 创建 axios 实例
const apiClient = axios.create({
  baseURL: 'https://anime-music.jijidown.com/api/v2/music', // 基础地址不带参数
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 封装获取推荐音乐的方法
export async function getRecommendMusic(): Promise<RecommendMusic> {
  try {
    const response: AxiosResponse<RecommendResponse> = await apiClient.get('', {
      params: { recommend: true } // 通过参数添加推荐标识
    });

    if (response.data.code !== 0) {
      throw new Error(`API Error: ${response.data.msg}`);
    }

    return response.data.res;
  } catch (error: any) {
    // 统一错误处理
    const errorMessage = error.response?.data?.msg 
                      || error.message 
                      || '未知错误';
    throw new Error(`获取推荐失败: ${errorMessage}`);
  }
}